using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Musicman.Entities;
using Musicman.Common;
namespace Musicman.DAL
{
	public static class SearchlocationDAO
	{
		#region Insert
		private static DbCommand insertCommand = null;
		private static string insertSQL = "INSERT INTO SearchLocation (SEARCHJOBID, NAME) VALUES (@SEARCHJOBID, @NAME) SET @SEARCHLOCATIONID = SCOPE_IDENTITY()";
		private static DbCommand GetInsertCommand(SqlDatabase db)
		{
			if (insertCommand == null)
			{
				insertCommand = db.GetSqlStringCommand(insertSQL);
				db.AddOutParameter(insertCommand, "@SEARCHLOCATIONID", DbType.Int32, 4);
				db.AddInParameter(insertCommand, "@SEARCHJOBID", DbType.Int64);
				db.AddInParameter(insertCommand, "@NAME", DbType.String);
			}
			return insertCommand;
		}
		private static void SetInsertValues(SqlDatabase db, DbCommand cmd, Searchlocation entity)
		{
			db.SetParameterValue(cmd, "@SEARCHJOBID", entity.Searchjobid);
			db.SetParameterValue(cmd, "@NAME", entity.Name);
		}
		public static bool InsertSearchlocation(Searchlocation entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			if (res)
			{
				entity.Searchlocationid = (int?)db.GetParameterValue(cmd, "@SEARCHLOCATIONID");
			}
			return res;
		}
		public static bool InsertSearchlocation(Searchlocation entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = false;
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			if (res)
			{
				entity.Searchlocationid = (int?)db.GetParameterValue(cmd, "@SEARCHLOCATIONID");
			}
			return res;
		}
		public static bool InsertListSearchlocation(List<Searchlocation> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
				DbCommand cmd = GetInsertCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Searchlocation entity = list[i];
					SetInsertValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
					if (res)
					{
						entity.Searchlocationid = (int?)db.GetParameterValue(cmd, "@SEARCHLOCATIONID");
					}
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool InsertListSearchlocation(List<Searchlocation> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			DbCommand cmd = GetInsertCommand(db);
			for(int i=0; i<list.Count; i++)
			{
				Searchlocation entity = list[i];
				SetInsertValues(db, cmd, entity);
				res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				if (res)
				{
					entity.Searchlocationid = (int?)db.GetParameterValue(cmd, "@SEARCHLOCATIONID");
				}
			}
			return res;
		}
		#endregion
		#region Update
		private static DbCommand updateCommand = null;
		private static string updateSQL = "UPDATE SearchLocation SET SEARCHJOBID = @SEARCHJOBID, NAME = @NAME WHERE SEARCHLOCATIONID = @SEARCHLOCATIONID";
		private static DbCommand GetUpdateCommand(SqlDatabase db)
		{
			if (updateCommand == null)
			{
				updateCommand = db.GetSqlStringCommand(updateSQL);
				db.AddInParameter(updateCommand, "@SEARCHLOCATIONID", DbType.Int32);
				db.AddInParameter(updateCommand, "@SEARCHJOBID", DbType.Int64);
				db.AddInParameter(updateCommand, "@NAME", DbType.String);
			}
			return updateCommand;
		}
		private static void SetUpdateValues(SqlDatabase db, DbCommand cmd, Searchlocation entity)
		{
			db.SetParameterValue(cmd, "@SEARCHLOCATIONID", entity.Searchlocationid);
			db.SetParameterValue(cmd, "@SEARCHJOBID", entity.Searchjobid);
			db.SetParameterValue(cmd, "@NAME", entity.Name);
		}
		public static bool UpdateSearchlocation(Searchlocation entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool UpdateSearchlocation(Searchlocation entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = false;
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			return res;
		}
		public static bool UpdateListSearchlocation(List<Searchlocation> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
				DbCommand cmd = GetUpdateCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Searchlocation entity = list[i];
					SetUpdateValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool UpdateListSearchlocation(List<Searchlocation> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			DbCommand cmd = GetUpdateCommand(db);
			for(int i=0; i<list.Count; i++)
			{
				Searchlocation entity = list[i];
				SetUpdateValues(db, cmd, entity);
				res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			}
			return res;
		}
		
		#endregion
		#region Save

        public static bool SaveSearchlocation(Searchlocation entity)
        {
            return SaveSearchlocation(entity, false);
        }

        public static bool SaveSearchlocation(Searchlocation entity, bool saveFolders)
        {
            bool res = true;
            DbTransaction transactionObj = null;
            DbConnection conn = null;
            try
            {
                SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
                DbCommand cmd = GetUpdateCommand(db);
                conn = db.CreateConnection();
                conn.Open();
                transactionObj = conn.BeginTransaction();

                if (entity.Searchlocationid == null)
                {
                    res = InsertSearchlocation(entity);
                }
                else
                {
                    res = UpdateSearchlocation(entity);
                }
                if (saveFolders)
                {
                    res = res & SearchfolderDAO.SaveListSearchfolder(entity.Folders, transactionObj, db, true);
                }
                transactionObj.Commit();
                transactionObj.Dispose();
                conn.Close();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                res = false;
                if (transactionObj != null)
                {
                    transactionObj.Rollback();
                    transactionObj.Dispose();
                }
                if (conn != null & conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                    conn.Dispose();
                }
                throw ex;
            }
			return res;
		}

        public static bool SaveSearchlocation(Searchlocation entity, DbTransaction transactionObj, SqlDatabase db)
        {
            return SaveSearchlocation(entity, transactionObj, db, false);
        }

		public static bool SaveSearchlocation(Searchlocation entity, DbTransaction transactionObj, SqlDatabase db, bool saveFolders)
		{
			bool res = true;
			if (entity.Searchlocationid == null )
			{
				res = InsertSearchlocation(entity, transactionObj, db);
			}
			else
			{
				res = UpdateSearchlocation(entity, transactionObj, db);
			}
            if (saveFolders)
            {
                res = res & SearchfolderDAO.SaveListSearchfolder(entity.Folders, transactionObj, db, true);
            }
            return res;
		}

        public static bool SaveListSearchlocation(List<Searchlocation> list)
        {
            return SaveListSearchlocation(list, false);
        }

        public static bool SaveListSearchlocation(List<Searchlocation> list, bool saveFolders)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Searchlocation entity = list[i];
					res = res & SaveSearchlocation(entity, transactionObj, db);
                    if (saveFolders)
                    {
                        res = res & SearchfolderDAO.SaveListSearchfolder(entity.Folders, transactionObj, db, true);
                    }
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}

        public static bool SaveListSearchlocation(List<Searchlocation> list, DbTransaction transactionObj, SqlDatabase db)
        {
            return SaveListSearchlocation(list, transactionObj, db, false);
        }

        public static bool SaveListSearchlocation(List<Searchlocation> list, DbTransaction transactionObj, SqlDatabase db, bool saveFolders)
		{
			bool res = true;
			for(int i=0; i<list.Count; i++)
			{
				Searchlocation entity = list[i];
				res = res & SaveSearchlocation(entity, transactionObj, db);
                if (saveFolders)
                {
                    res = res & SearchfolderDAO.SaveListSearchfolder(entity.Folders, transactionObj, db, true);
                }
			}
			return res;
		}
		#endregion

		#region Delete
		private static DbCommand deleteCommand = null;
		private static string deleteSQL = "DELETE FROM SearchLocation WHERE SEARCHLOCATIONID = @SEARCHLOCATIONID";
		private static DbCommand GetDeleteCommand(SqlDatabase db)
		{
			if (deleteCommand == null)
			{
				deleteCommand = db.GetSqlStringCommand(deleteSQL);
				db.AddInParameter(deleteCommand, "@SEARCHLOCATIONID", DbType.Int32);
			}
			return deleteCommand;
		}
		private static void SetDeleteValues(SqlDatabase db, DbCommand cmd, Searchlocation entity)
		{
			db.SetParameterValue(cmd, "@SEARCHLOCATIONID", entity.Searchlocationid);
		}
		public static bool DeleteSearchlocation(Searchlocation entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
			DbCommand cmd = GetDeleteCommand(db);
			SetDeleteValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool DeleteListSearchlocation(List<Searchlocation> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
				DbCommand cmd = GetDeleteCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Searchlocation entity = list[i];
					SetDeleteValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		
		#endregion
		#region Select
		private static string selectSQL = "SELECT SEARCHLOCATIONID,SEARCHJOBID,NAME FROM SearchLocation {0} {1}";
		
		public static List<Searchlocation> LoadSearchlocations(string where, string orderby)
		{
			List<Searchlocation> res = new List<Searchlocation>();
			string sql = String.Format(selectSQL, where, orderby);
			IDataReader reader = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
				reader = db.ExecuteReader(CommandType.Text, sql);
				if (reader != null)
				{
					while (reader.Read())
					{
						Searchlocation entity = new Searchlocation();
						entity.Searchlocationid = reader.IsDBNull(0) ? null : (int?)reader.GetInt32(0);
						entity.Searchjobid = reader.IsDBNull(1) ? null : (long?)reader.GetInt64(1);
						entity.Name = reader.IsDBNull(2) ? null : (string)reader.GetString(2);
						res.Add(entity);
					}
				}
				reader.Close();
			}
			catch (Exception ex)
			{
				if (reader != null && !reader.IsClosed)
				{
					reader.Close();
				}
				res = null;
				throw ex;
			}
			return res;
		}
		#endregion
	}
}
